   package com.javalong.affairs.module.notifything.activity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.MessageInfo;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TextSelectView.OnImageClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_updatemsg)
public class UpdateMsgActivity extends UIHandlerActivity implements OnImageClick {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.et_address)
	private EditText et_address;
	@ViewInject(R.id.et_content)
	private EditText et_content;
	@ViewInject(R.id.bt_send)
	private Button bt_send;
	@ViewInject(R.id.ui_tsview_receiver)
	private TextSelectView tsView_receiver;
	@ViewInject(R.id.et_title)
	private EditText et_title;
	@ViewInject(R.id.bt_settime)
	private Button bt_settime;
	@ViewInject(R.id.et_timelimit)
	private EditText et_timelimit;
	@ViewInject(R.id.tv_sender)
	private TextView tv_sender;
	private Date nowDate;
	private StringBuilder receiverStr = new StringBuilder();
	private String msgId;
	private String simpleMsgId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		nowDate = new Date();
		dateTimePicKDialog = new DateTimePickDialogUtil(this,
				DateUtils.formatDate(nowDate));
		topTitle.setOnLeftDrawableClick(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		msgId = bundle.getString(BundleKey.MSGID);
		simpleMsgId = bundle.getString(BundleKey.SIMPLEMSGID);
		initData();

	}

	private void initData() {
		String url = UrlUtils.getUrl("SendMessage", "outOne_messageinfo");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(DataType.MESSAGE) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				initUI((MessageInfo) obj);
			}
		});
	}

	private void initUI(MessageInfo msgInfo) {
		if (msgInfo == null) {
			return;
		}
		if ("0".equals(msgInfo.time)) {
			tv_time.setText(DateUtils.formatDate(nowDate));
		} else {
			nowDate = new Date(Long.parseLong(msgInfo.time) * 1000);
			tv_time.setText(DateUtils.formatDate(nowDate));
		}
		tsView_receiver.setOnImageClick(this);
		bt_send.setOnClickListener(this);

		bt_settime.setOnClickListener(this);
		et_content.setText(msgInfo.messageData);
		et_content.setOnClickListener(this);
		et_address.setText(msgInfo.place);
		et_title.setText(msgInfo.title);
		et_timelimit.setText(msgInfo.timeLimit);
		tv_sender.setText(msgInfo.sender);
	}

	private DateTimePickDialogUtil dateTimePicKDialog;

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.ui_tsview_receiver:
			IntentManager.doActivityForResult(UpdateMsgActivity.this,
					ReceiverActivity.class, RequestCode.RECEIVER);
			break;
		case R.id.bt_settime:
			dateTimePicKDialog.dateTimePicKDialog(tv_time);
			break;
		case R.id.bt_send:
			User user = (User) AllData.GetInstance().get(
					R.string.data_user_current_user);
			String url = UrlUtils.getUrl("SendMessage", "update");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.ID, simpleMsgId);
			params.put(RequestKey.MESSAGEDATA, et_content.getText().toString()
					.toString());
			params.put(RequestKey.TITLE, et_title.getText().toString());
			if (dateTimePicKDialog.getSeconds() == 0) {
				params.put(RequestKey.TIME, nowDate.getTime() / 1000);
			} else {
				params.put(RequestKey.TIME, dateTimePicKDialog.getSeconds());
			}
			params.put(RequestKey.PLACE, et_address.getText().toString());
			params.put(RequestKey.SENDER, user.userName);
			params.put(RequestKey.RECIPIENT, receiverStr.toString());
			params.put(RequestKey.ATTACHMENT, "");
			params.put(RequestKey.TIMELIMIT, et_timelimit.getText().toString());

			HttpUtils.post(url, params, new BaseResponseListener(
					new TypeToken<User>() {
					}.getType()) {
				@Override
				public void onSuccess(Object obj) {
					super.onSuccess(obj);
					ServiceLocator
							.GetInstance()
							.getInstance(UIHandler.class)
							.postCallback(
									R.string.event_refresh_notification_send,
									null);
					finish();
				}
			});
			break;
		case R.id.et_content:
			Bundle bundle = new Bundle();
			bundle.putString(BundleKey.EDITCONTENT, et_content.getText()
					.toString());
			IntentManager.doActivityForResult(this, EditContentActivity.class,
					RequestCode.EDITCONTENT, bundle);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RequestCode.RECEIVER:
				List<User> userList = (List<User>) data
						.getSerializableExtra(BundleKey.USERLIST);
				StringBuilder sb = new StringBuilder();
				for (User u : userList) {
					sb.append(u.userName).append(";");
					receiverStr.append(u.userName).append(",");
				}
				receiverStr.deleteCharAt(receiverStr.length() - 1);
				sb.deleteCharAt(sb.length() - 1);
				tsView_receiver.setText(sb.toString());
				break;
			case RequestCode.EDITCONTENT:
				String editContent = data.getStringExtra(BundleKey.EDITCONTENT);
				et_content.setText(editContent);
				break;
			}
		}
	}
}
